// sensordatathread.cpp
#include "sensordatathread.h"
#include <QRandomGenerator>
#include <QThread>

SensorDataThread::SensorDataThread(QObject *parent)
    : QThread(parent)
    , m_running(true)
    , m_startTime(QDateTime::currentDateTime())
{
}

void SensorDataThread::stop()
{
    m_running = false;
    wait(); // 等待线程完成当前工作
}

void SensorDataThread::run()
{
    while (m_running) {
        // 计算从开始到现在的时间
        QDateTime currentTime = QDateTime::currentDateTime();
        double elapsedSeconds = m_startTime.msecsTo(currentTime) / 1000.0;

        // 模拟传感器数据 (在子线程中生成)
        double sensorValueX = QRandomGenerator::global()->bounded(-5, 5);
        double sensorValueY = QRandomGenerator::global()->bounded(-5, 5);
        double sensorValueZ = QRandomGenerator::global()->bounded(-5, 5);

        // 发送数据到主线程
        emit sensorDataUpdated(elapsedSeconds, sensorValueX, sensorValueY, sensorValueZ);

        // 暂停100ms (10Hz 更新率)
        msleep(100);
    }
}
